<!--
 * @Author: your name
 * @Date: 2021-12-29 21:52:08
 * @LastEditTime: 2022-01-05 18:50:15
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \am-ui\src\pages\am\device\List.vue
-->
<template>
  <div class="device">
    <!-- 按钮区域 -->
     <div class="btns">
      
       <el-form :inline="true" :model="params" class="demo-form-inline" size="small">
          <el-form-item >
            <el-input v-model="params.name" placeholder="请输入设备的关键字" clearable></el-input>
          </el-form-item>
          <el-form-item >
            <el-select v-model="params.engineer_id" placeholder="请选择工程" clearable>
            <el-option v-for="(item,index) in engineers" :key="index" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
           <el-form-item >
            <el-select v-model="params.bind_status" placeholder="请选择绑定状态" clearable>
            <el-option  label="已绑定" :value="1"></el-option>
            <el-option label="未绑定" :value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item >
            <el-select v-model="params.online_status" placeholder="请选择在线状态" clearable>
            <el-option label="在线" :value="1"></el-option>
            <el-option label="离线" :value="0"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="toSearchHandler">查询</el-button>
        </el-form-item>
            <el-button  @click="toAddHandler" type="primary" size="small" >添加</el-button>
        </el-form>

     </div>

    <!-- 表格区域 -->
    <div class="table">
      <el-table size="small" :data="resData.list" style="width:100%" >
        <el-table-column  label="序号" width="60px" type="index" align="center"></el-table-column>
        <el-table-column prop="serial_number" label="设备编号" width="160px" align="center"></el-table-column>
        <el-table-column prop="name" label="设备名称" width="160px" align="center"></el-table-column>
        <el-table-column prop="bind_status" label="绑定状态" width="130px" align="center">
          <template v-slot="scope">
            <el-tag v-if="scope.row.bind_status == 1" type="success" size="mini">绑定</el-tag>
            <el-tag v-else type="danger" size="mini">未绑定</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="online_status" label="在线状态" width="80px" align="center">
          <template v-slot="scope">
            <el-tag v-if="scope.row.online_status == 1" type="success" size="mini">在线</el-tag>
            <el-tag v-else type="danger" size="mini">离线</el-tag>
        </template>
        </el-table-column>
        <el-table-column prop="latitude" label="经度" width="150px" align="center" >
          <template v-slot="scope">
             <el-tag v-if="scope.row.latitude == null" type="info" size="mini">暂未绑定</el-tag>
             <span v-else>{{scope.row.latitude}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="longitude" label="维度" width="150px" align="center">
          <template v-slot="scope">
              <el-tag v-if="scope.row.longitude == null" type="info" size="mini">暂未绑定</el-tag>
              <span>{{scope.row.longitude}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="video" label="视频" width="100px" align="center">
          <template v-slot="scope">
            <el-tag v-if="scope.row.video == null" type="info" size="mini">暂未开通</el-tag>
            <span>{{scope.row.video}}</span>    
          </template>
        </el-table-column>
        <el-table-column label="操作"  align="center">
          <template v-slot="{row}">
            <el-button @click="toEditHandler(row)" type="text">编辑</el-button>
            <el-button @click="toDeleteHandler(row.id)" type="text" style="color:red" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页表格区域 -->
     <el-pagination
     background
     style="margin-top:1em"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="parseInt(resData.page)"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="parseInt(resData.pageSize)"
      layout="total, sizes, prev, pager, next, jumper"
      :total="parseInt(resData.total)">
    </el-pagination>

    <!-- 模态框区域 -->
    <el-dialog @close="dialogCloseHandler" :title="title" :visible.sync="visible">
      <el-form :model="form" :rules="rules" ref="deviceFrom">
      <el-form-item prop="name" label="设备名称" :label-width="formLabelWidth">
        <el-input v-model="form.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item prop="serial_number" label="设备编号" :label-width="formLabelWidth">
        <el-input v-model="form.serial_number" autocomplete="off"></el-input>
      </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="visible = false">取 消</el-button>
    <el-button type="primary" @click="toSubmitHandler" >确 定</el-button>
  </div>
</el-dialog>
  </div>
</template>

<script>
//导入工具插件
import { get,postJSON,del } from '@/utils/request.js'
// import { log } from 'node_modules/_@antv_g2plot@2.4.5@@antv/g2plot/lib/utils';
export default {
  data() {
    return {
       resData: {},
       params: {
         page:1,
         pageSize:10,
       },
       visible:false,//模态框显示与隐藏
       form:{},
       formLabelWidth:"120px", //表单左侧label的宽度
       rules: {
         name: [{ required: true, message:"请输入设备名称", trigger:"blur"}
         ],
         serial_number:[
           {required: true, message:"请输入设备编号", trigger:"blur"}
         ],
       },
       title:'',//模态框的标题
       engineers:''
    };
  },
  methods: {
    //分页查询设备信息
    pageQueryDevices() {
      get('/device/pageQuery',this.params).then((res)=>{
        // console.log(res.data.list);
        this.resData = res.data;
      });
    },
    //pageSize改变事件
    handleSizeChange(pageSize) {
      
      //将分页插件最新的参数赋值给params
      this.params.pageSize = pageSize;
      //调用查询设备的方法（数据重载）
      this.pageQueryDevices();
    },
    //page改变事件
    handleCurrentChange(page) {
      // console.log(page);
      this.params.page = page;
      this.pageQueryDevices();
    },
    //添加按钮事件
    toAddHandler(){
      // 给模态框添加标题
      this.title="添加设备"
      //将表单置空
      this.form={};
      //打开模态框
      this.visible=true;
    },
    // 表单提交事件
    toSubmitHandler() {
      this.$refs["deviceFrom"].validate((valid)=> {
        if (valid) {
           postJSON("/device/saveOrUpdate", this.form).then((res) => {
             // 数据重载
              this.pageQueryDevices();
             // 成功提示
             this.$message({
                message:"操作成功",
                type:"success",
         }) ;
        // 模态框关闭
        this.visible = false;
      });
      
    }else {
      console.log("error submit!!");
      return false;
      }
    });
  },
  //模态框关闭
  dialogCloseHandler() {
    //清空表单验证
    this.$refs["deviceFrom"].resetFields();
  },
  //编辑按钮点击事件
  toEditHandler(row) {
    //将当前行信息赋值给表单
    this.form= {
      id:row.id,
      name:row.name,
      serial_number:row.serial_number,
    };
    this.title="修改设备";
    this.visible=true;
  },
  //查询所有工程信息
  findAllengineers() {
    get('engineer/pageQuery',{
      page:1,
      pageSize:100,
    }).then((res)=>{
      this.engineers=res.data.list
    })
  },
  //查询按钮事件
  toSearchHandler() {
    //数据重载
    this.pageQueryDevices();
  },
  //删除事件
  toDeleteHandler(id) {
        this.$confirm('此操作将永久删除设备, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(()=>{
          //用户点击确认按钮,然后再做设备的删除
          del("/device/deleteById",{id}).then((res)=>{
            //数据重载
            this.pageQueryDevices();
            //提示删除成功
            this.$message.success('删除成功')
          })
        })
  }
},
  //vue声明周期钩子函数
  created() {
    //页面加载，调用查询设备
    this.pageQueryDevices();
    //查询所有工程信息
    this.findAllengineers();
  },
};
</script>